package cn.cau.control;

import cn.cau.dao.BasicEnumDao;
import cn.cau.dao.CategoryDao;
import cn.cau.entity.*;
import cn.cau.service.*;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import static org.springframework.asm.Type.getType;

@Controller
public class CodeDesignController {
    @Autowired
    private CodeManageChangeService codeManageChangeService;
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BasicEnumService basicEnumService;
    @Autowired
    private NonEnumService nonEnumService;


    @RequestMapping(value = "/codeDesignIndex", method = RequestMethod.GET)
    public String codeDesignIndex() {
        return "codeDesign/codeDesignIndex";
    }

    @RequestMapping(value = "/commonDesign", method = RequestMethod.GET)
    public ModelAndView commonDesign(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();

        if (session.getAttribute("userInfo") == null) {
            modelAndView.setViewName("codeDesign/codeDesignIndex");
            return modelAndView;
        }//如果用户登录
        CommonUser commonUser = (CommonUser) session.getAttribute("userInfo");
        modelAndView.addObject("userId",commonUser.getUserId());
        modelAndView.addObject("userName",commonUser.getUserName());
        modelAndView.setViewName("codeDesign/commonDesign");
        return modelAndView;
    }

    @RequestMapping(value = "/commonDesignSave", method = RequestMethod.POST)
    public ModelAndView saveCodeRule(HttpServletRequest request,
                               HttpSession session,
                               @RequestParam(value = "code_names[]")List<String> code_names,
                               @RequestParam(value = "code_categorys[]")List<String> code_categorys,
                               @RequestParam(value = "code_nums[]")List<String> code_nums,
                               @RequestParam(value = "details[]") ArrayList<String> details,
                               @RequestParam(value = "detailsname[]") ArrayList<String> detailsnames){
        ModelAndView modelAndView = new ModelAndView();
        Category cg=new Category();
        cg.setCategoryId(0);
        cg.setRuleCategoryId(Integer.valueOf(request.getParameter("categoryOptions").toString()));
        cg.setCategory(request.getParameter("category"));

        String cgcategory=request.getParameter("categorydetail");
        if (cgcategory.length()==1){
            cgcategory="0"+cgcategory;
        }

        cg.setDetails(cgcategory);

        int res=categoryService.insertCategory(cg);
        CodeRule cr=new CodeRule();
        cr.setCodeRuleId(0);
        cr.setCategoryId(cg.getCategoryId());
        cr.setRuleCategoryId(Integer.valueOf(request.getParameter("categoryOptions").toString()));
        cr.setCodeCodeCount(Integer.valueOf(request.getParameter("codeCodeCount").toString()));
        cr.setCodeRuleName(request.getParameter("codeDesignName"));
        cr.setCodeRuleIntro(request.getParameter("codeDescribe"));
        cr.setSerialNumber(Integer.valueOf(request.getParameter("serial_number")));
        //设置categoryId
        cr.setCategoryId(cg.getCategoryId());
        cr.setCodeState(1);

        //codeRule入库
        res=codeRuleService.insertCodeRule(cr);
        Iterator it1 = code_names.iterator();
        Iterator it2 = code_categorys.iterator();
        Iterator it3 = code_nums.iterator();
        Iterator it4 = details.iterator();
        Iterator it5 = detailsnames.iterator();

        int count=0;
            while (it1.hasNext()){
                String code_name=it1.next().toString();
                String code_category=it2.next().toString();
                String code_num=it3.next().toString();
                String detail=it4.next().toString();
                String detailsname=it5.next().toString();
                //码段名称

                if(code_names.size()==1 ){
                    while(it4.hasNext()){
                        detail=detail+","+it4.next();
                        detailsname=detailsname+","+it5.next();
                    }
                }
                if (Integer.valueOf(code_category)==1){
                    //枚举类
                    count=count+1;
                    String[] basicEnums=detail.split(",");
                    String[] basicEnumsNames=detailsname.split(",");
                    for (int i=0;i< basicEnums.length;i++){
                        BasicEnum bs=new BasicEnum();
                        bs.setCodeCodeId(count);
                        bs.setCodeRuleId(cr.getCodeRuleId());
                        bs.setBasicEnumeration(basicEnums[i]);
                        bs.setDetailsname(basicEnumsNames[i]);
                        bs.setDetails(code_name);
                        bs.setCodeLength(Integer.valueOf(code_num));
                        res=basicEnumService.insertBasicEnum(bs);
                    }
                } else {
                    //非枚举类
                    count=count+1;
                    NonEnum ne=new NonEnum();
                    ne.setCodeCodeId(count);
                    ne.setCodeRuleId(cr.getCodeRuleId());
                    ne.setDetails(code_name);
                    String upperLimit=detail.split(",")[0];
                    String lowerLimit=detail.split(",")[1];
                    ne.setUpperLimit(upperLimit);
                    ne.setLowerLimit(lowerLimit);
                    ne.setCodeLength(Integer.valueOf(code_num));
                    res=nonEnumService.insertNonEnum(ne);
                }
            }


        //获取用户
        CommonUser commonUser = (CommonUser) session.getAttribute("userInfo");
        //管理表
        CodeManageRecord codeManageRecord=new CodeManageRecord();
        codeManageRecord.setNewCodeRuleId(cr.getCodeRuleId());
        codeManageRecord.setManageManId(commonUser.getUserId());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        codeManageRecord.setApplyTime(df.format(new Date()));
        codeManageRecord.setManageOperation(2);
        int result=codeManageChangeService.submitCodeRuleChange(codeManageRecord);
        result=result+1;
        modelAndView.addObject("result",result);
        modelAndView.setViewName("codeDesign/codeDesignIndex");
        return modelAndView;
    }

    @RequestMapping(value = "/workFlowDesign", method = RequestMethod.GET)
    public ModelAndView workFlowDesignSave(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();

        if (session.getAttribute("userInfo") == null) {
            modelAndView.setViewName("codeDesign/codeDesignIndex");
            return modelAndView;
        }//如果用户登录
        CommonUser commonUser = (CommonUser) session.getAttribute("userInfo");
        modelAndView.addObject("userId",commonUser.getUserId());
        modelAndView.addObject("userName",commonUser.getUserName());
        modelAndView.setViewName("codeDesign/workFlowDesign");
        return modelAndView;
    }

    @RequestMapping(value = "/workFlowDesignSave", method = RequestMethod.POST)
    public ModelAndView saveWorkFlowCodeRule(HttpServletRequest request,
                                     HttpSession session,
                                     @RequestParam(value = "code_names[]")List<String> code_names,
                                     @RequestParam(value = "code_categorys[]")List<String> code_categorys,
                                     @RequestParam(value = "code_nums[]")List<String> code_nums,
                                     @RequestParam(value = "details[]") ArrayList<String> details,
                                     @RequestParam(value = "detailsname[]") ArrayList<String> detailsnames){
        ModelAndView modelAndView = new ModelAndView();
        Category cg=new Category();
        cg.setCategoryId(0);
        cg.setRuleCategoryId(9);
        cg.setCategory(request.getParameter("category"));
        String cgcategory=request.getParameter("categorydetail");
        if (cgcategory.length()==1){
            cgcategory=0+cgcategory;
        }
        cg.setDetails(cgcategory);

        int res=categoryService.insertCategory(cg);
        CodeRule cr=new CodeRule();
        cr.setCodeRuleId(0);
        cr.setCategoryId(cg.getCategoryId());
        cr.setRuleCategoryId(9);
        cr.setCodeCodeCount(Integer.valueOf(request.getParameter("codeCodeCount").toString()));
        cr.setCodeRuleName(request.getParameter("codeDesignName"));
        cr.setCodeRuleIntro(request.getParameter("codeDescribe"));
        cr.setSerialNumber(Integer.valueOf(request.getParameter("serial_number")));
        //设置categoryId
        cr.setCategoryId(cg.getCategoryId());
        cr.setCodeState(1);
        //codeRule入库
        res=codeRuleService.insertCodeRule(cr);
        Iterator it1 = code_names.iterator();
        Iterator it2 = code_categorys.iterator();
        Iterator it3 = code_nums.iterator();
        Iterator it4 = details.iterator();
        Iterator it5 = detailsnames.iterator();
        int count=0;
        while (it1.hasNext()){

            //码段名称
            String code_name=it1.next().toString();
            String code_category=it2.next().toString();
            String code_num=it3.next().toString();
            String detail=it4.next().toString();
            String detailsname=it5.next().toString();
            if(code_names.size()==1){
                while(it4.hasNext()){
                    detail=detail+","+it4.next();
                    detailsname=detailsname+","+it5.next();
                }
            }

            //枚举类
            count=count+1;
            String[] basicEnums=detail.split(",");
            String[] basicEnumsNames=detailsname.split(",");
            for (int i=0;i< basicEnums.length;i++){
                BasicEnum bs=new BasicEnum();
                bs.setCodeCodeId(count);
                bs.setCodeRuleId(cr.getCodeRuleId());
                bs.setBasicEnumeration(basicEnums[i]+"0");
                bs.setDetailsname(basicEnumsNames[i]);
                bs.setDetails(code_name);
                bs.setCodeLength(3);
                res=basicEnumService.insertBasicEnum(bs);
            }

        }
        //获取用户
        CommonUser commonUser = (CommonUser) session.getAttribute("userInfo");
        //管理表
        CodeManageRecord codeManageRecord=new CodeManageRecord();
        codeManageRecord.setNewCodeRuleId(cr.getCodeRuleId());
        codeManageRecord.setManageManId(commonUser.getUserId());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        codeManageRecord.setApplyTime(df.format(new Date()));
        codeManageRecord.setManageOperation(2);
        int result=codeManageChangeService.submitCodeRuleChange(codeManageRecord);
        result=result+1;
        modelAndView.addObject("result",result);
        modelAndView.setViewName("codeDesign/codeDesignIndex");
        return modelAndView;
    }
}
